<?php
class Myvalidate_Exist extends Zend_Validate_Abstract
{
    const NOTEXIST = 'notExist';
    protected $_messageTemplates = array(self::NOTEXIST => "'%value%' doesn't exist.");
    private $tableName;
    private $field;
    public function isValid ($value)
    {
        $this->_setValue($value);
        $id = $this->checkUnique($value);
        if (!$id) {
            $this->_error();
            return false;
        }
        return true;
    }
    public function setTableName ($table)
    {
        $this->tableName = $table;
    }
    public function setFieldName ($fie)
    {
        $this->field = $fie;
    }
    public function checkUnique ($value)
    {
        $db_r = Zend_Registry::get("db_r");
        $select = $db_r->select();
        $select->from($this->tableName, '*');
        $key = $this->field;
        $select->where("$key=?", $value);
        $select->where("is_delete=?", '0');
        $sql = $select->__toString();
        return $db_r->fetchOne($sql);
    }
}

